<template>
    <div class="com-container">                
        <!-- 标题 -->
        <div class="tittle" :style="comStyle" >
            <span class="before-icon">▎</span>
            <strong>{{showTitle}}</strong>
            <span class="iconfont title-icon" @click="showMenu = !showMenu" :style="comStyle">&#xe6eb;</span>
            <div class="select-con" v-show="showMenu" :style="[marginStyle,bgcStyle]">
                <div class="select-item" v-for="item in selectTypes" :key="item.key"  @click.prevent="handleSelect(item.key)">
                    {{item.text}}
                </div>
            </div>
        </div>      
        <!-- 显示图表 -->
        <div class="com-chart" ref="trend_ref">

        </div>
    </div>
</template>

<script>
import { mapState } from 'vuex'
import { getThemeValue } from '@/utils/theme_utils'
export default {
    data() {
        return {
            chartInstance:null,
            //服务器获取数据
            allData:null,
            // 是否显示可选项
            showMenu: false,
            //显示的数据类型
            choicType:'map',
            //标题字体大小
            titleFontSize: 0,
        }
    },
    created(){
        //回调函数的注册
        this.$socket.registerCallBack('trendData',this.getData)
    },
    mounted() {
        this.initChart()
        // this.getData()
        //获取数据 发送数据给服务器
        this.$socket.send({
            action:'getData',
            socketType:'trendData',
            chartName:'trend',
            value:''
        })
        //分辨率适配
        window.addEventListener('resize',this.screenAdapter)
        this.screenAdapter()
    },
    destroyed(){
        //取消监听
        window.removeEventListener('resize', this.screenAdapter)
        //回调函数的取消
        this.$socket.unRegisterCallBack("trendData")
    },
    methods: {
         initChart(){
            this.chartInstance = this.$echarts.init(this.$refs.trend_ref,this.theme)
            const initOption = {
                grid:{
                    top:'35%',
                    left:'3%',
                    right:'4%',
                    bottom:'1%',
                    //距离坐标轴的文字
                    containLabel:true
                },
                //鼠标触发工具提示
                tooltip:{
                    trigger:'axis'
                },
                //调整图例位置
                legend:{
                    left:'center',
                    top:'15%',
                    icon:'circle'
                },
                xAxis:{
                    type:'category',
                    boundaryGap:false
                    },
                yAxis:{
                    type:'value'
                }
            }
            this.chartInstance.setOption(initOption)
         },
         //ret 服务端发送给客户端的数据
         async getData(res){
                // const {data:res} =  await this.$http.get('trend')
                console.log(res)
                this.allData = res
                this.updateChart()
         },
         updateChart(){
            // 半透明的颜色值
            const colorArrHalfOpacity = ['rgba(11, 168, 44, 0.5)', 'rgba(44, 110, 255, 0.5)', 'rgba(22, 242, 217, 0.5)', 'rgba(254, 33, 30, 0.5)', 'rgba(250, 105, 0, 0.5)']
            // 全透明的颜色值
            const colorArrAllOpacity = ['rgba(11, 168, 44, 0)', 'rgba(44, 110, 255, 0)', 'rgba(22, 242, 217, 0)', 'rgba(254, 33, 30, 0)', 'rgba(250, 105, 0, 0)']
            //处理数据
            //X轴数据类目轴
            const timeArr = this.allData.common.month
            //Y轴数据数据轴
            const valueArr = this.allData[this.choicType].data
            const seriesArr = valueArr.map((item,index)=>{
                    return {
                        name: item.name,
                        type:'line',//折线
                        data:item.data,
                        //堆叠
                        stack:this.choicType,
                        //颜色填充
                        areaStyle:{
                            //渐变方向 x1 y1 x2 y2
                            color:new this.$echarts.graphic.LinearGradient(0, 0, 0, 1,[
                                // 0% 颜色
                                {
                                    offset: 0,
                                    color: colorArrHalfOpacity[index]
                                },
                                // 100% 颜色
                                {
                                    offset: 1,
                                    color: colorArrAllOpacity[index]
                                }
                            ])
                        }
                    }
                })
            //图例的数据
            const legendArr = valueArr.map((item)=>{
                    return item.name
            })
            const dataOption = {
                    xAxis:{
                        data:timeArr
                    },
                    //折线图图例
                    legend:{
                        data:legendArr
                    },
                    series:seriesArr
            }
            this.chartInstance.setOption(dataOption)
         },
         screenAdapter(){
            this.titleFontSize = this.$refs.trend_ref.offsetWidth/100 * 3.6
            const adapterOption = {
                legend: {
                    itemWidth: this.titleFontSize,
                    itemHeight: this.titleFontSize,
                    // 间距
                    itemGap: this.titleFontSize,
                    textStyle: {
                        fontSize: this.titleFontSize /1.3
                    }
                }
            }
            this.chartInstance.setOption(adapterOption)
            this.chartInstance.resize()

         },
         //选择变化索要展示的表格类型
         handleSelect(type){
                this.choicType = type
                this.updateChart()
                this.showMenu = false
         }
    },
    computed:{
        //展开列表切换的展示
        selectTypes(){
            if (!this.allData){
                return[]
            }else{
                return this.allData.type.filter(item =>item.key !== this.choicType)
            }
        },
        //显示标题计算属性
        showTitle() {
            if (!this.allData){
                return ''
            }else{
                return this.allData[this.choicType].title
             }          
        },
        // 设置给标题的大小
        comStyle() {
            return {
                fontSize: this.titleFontSize + 'px',
                color: getThemeValue(this.theme).titleColor
            }
        },
        marginStyle(){
            return {
                marginLeft:this.titleFontSize + 'px'
            }
        },
        bgcStyle(){
            return {
                backgroundColor:getThemeValue(this.theme).selectBackgroundColor
            }
        },
        ...mapState(['theme'])

    },
    watch: {
    theme () {
      console.log('主题切换了')
      this.chartInstance.dispose() // 销毁当前的图表
      this.initChart() // 重新以最新的主题名称初始化图表对象
      this.screenAdapter() // 完成屏幕的适配
      this.updateChart() // 更新图表的展示
    }
  }
}
</script>

<style lang="less" scoped>
    .tittle{       
        position: absolute;
        left: 50px;
        top: 20px;
        z-index: 10;
        color: white;
        // cursor: pointer;     
        .title-icon {
            margin-left: 10px;
            cursor: pointer;
        }
        .select-item{
            cursor: pointer;
        }
    }
    // .select-con{
    //     background-color: #222733;
    // }
</style>